
package sf.hmg.turntest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.util.Log;

public class PhotoPageFactory implements IPageFactory {
	final private String filePath = "/mnt/sdcard/image";
	//final private String filePath = "/sdcard/Photo/Photo Album";
	//final private String filePath = "/sdcard/TimeMemory/picture_thunbnail";
	private File[] photoList;
	private ArrayList<String> photoPathList;
	private Context context = null;

	private int photoNum = 0;
	private int photoIndex = 0;
	
	private int mWidth;
	private int mHeight;
	private int m_backColor = 0xffff9e85; // ������ɫ
	
	private Paint mPaint;

	private Bitmap curPhoto;
	private Bitmap m_book_bg = null;

	public PhotoPageFactory(int w, int h, Context mContext) {
		mWidth = w;
		mHeight = h;
		context = mContext;
		photoPathList = new ArrayList<String>();
	}
	
	@Override
	public void openFile(String strFilePath) throws IOException {
		File f = new File(filePath);
		photoList = f.listFiles();
		String photoPath;
		for(File file:photoList){
			if(!file.isFile())
				continue;
			photoPath = file.getPath();
			if(photoPath.substring(photoPath.indexOf(".") + 1, photoPath.length()).equals("png")){
				photoPathList.add(photoPath);
			}
		}
		photoNum = photoPathList.size();
	}

	@Override
	public void prePage() throws IOException {
		if (photoIndex > 0)
			curPhoto = BitmapFactory.decodeFile(photoPathList.get(--photoIndex));
	}

	@Override
	public void nextPage() throws IOException {
		if (photoIndex < photoNum - 1)
			curPhoto = BitmapFactory.decodeFile(photoPathList.get(++photoIndex));
	}

	public void onDraw(Canvas c) {

		
		c.drawColor(m_backColor);
		if (m_book_bg == null)
			c.drawColor(m_backColor);
		else
			c.drawBitmap(BitmapZoom(m_book_bg, 350f, 340f), 10, 10, null);
			//c.drawBitmap(m_book_bg, 0, 0, null);
		if(curPhoto == null)
		{
//			BitmapFactory.Options optsa = new BitmapFactory.Options();
//		    optsa.inSampleSize = 10;
			curPhoto = BitmapFactory.decodeFile(photoPathList.get(photoIndex));
		}
		
		//c.drawBitmap(curPhoto, 0, 0, null);
		c.drawBitmap(BitmapZoom(curPhoto, 300f, 300f), 35, 30, null);

		Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setTextAlign(Align.LEFT);
		mPaint.setTextSize(28);
		mPaint.setColor(Color.BLACK);
		c.drawText("~~" + String.valueOf(photoIndex + 1) + "~~", 215, 650, mPaint);
		
		mPaint.breakText("", true, 30, null);
		String aa = "汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字";
		String bb;
		int a = 0;
		for(int i = 0;;i++){
			a = mPaint.breakText(aa, true, 200, null);
			if(a<=0)
				break;
			bb = aa.substring(0, a);
			aa = aa.substring(a);
			
			c.drawText(bb, 215, 450 + i * 25, mPaint);
		}
		
		Log.i("onDraw", String.valueOf(photoIndex));
	}

	@Override
	public void setBgBitmap(Bitmap BG) {
		m_book_bg = BG;
	}

	@Override
	public boolean isFirstPage() {
		return (photoIndex == 0);
	}

	@Override
	public boolean isLastPage() {
		return (photoIndex == photoNum - 1);
	}

	private Bitmap BitmapZoom(Bitmap image, float width, float height) {
		Matrix mMatrix = new Matrix();
		mMatrix.postScale(width / image.getWidth(), height / image.getHeight());
		return Bitmap.createBitmap(image, 0, 0, image.getWidth(),
				image.getHeight(), mMatrix, true);
	}

	/* (non-Javadoc)
	 * @see sf.hmg.turntest.IPageFactory#onClick(float, float)
	 */
	@Override
	public void onClick(float x, float y) {
		if(x > 35 && y > 30 && x < 335 && y < 330){
			Intent intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			File file = new File(photoPathList.get(photoIndex));
			intent.setDataAndType(Uri.fromFile(file), "image/*");
			context.startActivity(intent);
		}
	}

	public String getCurrentPic(){
		return photoPathList.get(photoIndex);
	}
}
